package cn.iocoder.yudao.module.resource.controller.admin.category;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.category.vo.ResourceCategoryPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.category.vo.ResourceCategoryRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.category.vo.ResourceCategorySaveReqVO;
import cn.iocoder.yudao.module.resource.convert.ResourceCategoryConvert;
import cn.iocoder.yudao.module.resource.dal.dataobject.category.ResourceCategoryDO;
import cn.iocoder.yudao.module.resource.service.category.ResourceCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资源分类")
@RestController
@RequestMapping("/resource/category")
@Validated
public class ResourceCategoryController {

    @Resource
    private ResourceCategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建资源分类")
    @PreAuthorize("@ss.hasPermission('resource:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody ResourceCategorySaveReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源分类")
    @PreAuthorize("@ss.hasPermission('resource:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody ResourceCategorySaveReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源分类")
    @Parameter(name = "id", description = "分类ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除资源分类")
    @Parameter(name = "ids", description = "分类ID列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('resource:category:delete')")
    public CommonResult<Boolean> deleteCategoryList(@RequestParam("ids") List<Long> ids) {
        categoryService.deleteCategoryList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源分类")
    @Parameter(name = "id", description = "分类ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:category:query')")
    public CommonResult<ResourceCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        ResourceCategoryDO category = categoryService.getCategory(id);
        return success(BeanUtils.toBean(category, ResourceCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源分类分页")
    @PreAuthorize("@ss.hasPermission('resource:category:query')")
    public CommonResult<PageResult<ResourceCategoryRespVO>> getCategoryPage(@Valid ResourceCategoryPageReqVO pageReqVO) {
        PageResult<ResourceCategoryDO> pageResult = categoryService.getCategoryPage(pageReqVO);
        return success(ResourceCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源分类 Excel")
    @PreAuthorize("@ss.hasPermission('resource:category:export')")
    public void exportCategoryExcel(@Valid ResourceCategoryPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceCategoryDO> list = categoryService.getCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源分类.xls", "数据", ResourceCategoryRespVO.class,
                BeanUtils.toBean(list, ResourceCategoryRespVO.class));
    }

}
